package com.glorysoft.tonglingxjpda.ui.function.adapter;

import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: SearchHintAdapter
 * Author: XJH
 * Date: 2021/9/1 10:47
 * Description:
 * History:
 */
public class SearchHintAdapter extends ArrayAdapter<String> {
    private static final String TAG = "SearchHintAdapter";

    private Filter mFilter;
    private List<String> mObj;

    public SearchHintAdapter(@NonNull Context context, int resource, List<String> obj) {
        super(context, resource,obj);
        this.mObj = obj;
    }

    @NonNull
    @Override
    public Filter getFilter() {
        if (mFilter == null){
            mFilter = new HintFiler();
        }
        return mFilter;
    }

    private class HintFiler extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            ArrayList<Object> suggestions = new ArrayList<>();
            for (String s : mObj) {
                suggestions.add(s);
                Log.e(TAG, "performFiltering: " + s );
            }

            FilterResults results = new FilterResults();
            results.values = suggestions;
            results.count = suggestions.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mObj = (List<String>) results.values;
            if (results.count > 0){
                notifyDataSetChanged();
            }else {
                notifyDataSetInvalidated();
            }
        }
    }
}
